package com.edu.exam.entity.facility;

import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 楼宇基础信息实体
 * 所属模块：F-08 场地基础信息管理模块
 * 对应数据库表：venue_building
 *
 * <p>楼宇基础信息实体，管理教学楼宇的基本信息。
 * 用于存储楼宇的编码、名称、地址、楼层等基础信息，支持教室管理模块。</p>
 *
 * @author System
 * @version 1.0.0
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Entity
@Comment("楼宇基础信息表")
@SQLDelete(sql = "UPDATE venue_building SET is_deleted = true, delete_time = CURRENT_TIMESTAMP WHERE id = ?")
@Where(clause = "is_deleted = false")
public class Building {

    /**
     * 楼宇唯一标识符
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false)
    @Comment("楼宇唯一标识符")
    private Long id;

    /**
     * 楼宇编码，格式：CAM+年份+3位流水号
     */
    @Column(name = "building_code", nullable = false, unique = true, length = 50)
    @Comment("楼宇编码，格式：CAM+年份+3位流水号")
    private String buildingCode;

    /**
     * 楼宇名称
     */
    @Column(name = "building_name", nullable = false, length = 200)
    @Comment("楼宇名称")
    private String buildingName;

    /**
     * 楼宇简称
     */
    @Column(name = "building_short_name", length = 100)
    @Comment("楼宇简称")
    private String buildingShortName;

    /**
     * 校区代码
     */
    @Column(name = "campus_code", nullable = false, length = 50)
    @Comment("校区代码")
    private String campusCode;

    /**
     * 详细地址信息
     */
    @Column(name = "address", length = 500)
    @Comment("详细地址信息")
    private String address;

    /**
     * 总面积，单位：平方米
     */
    @Column(name = "total_area", precision = 10, scale = 2)
    @Comment("总面积，单位：平方米")
    private BigDecimal totalArea;

    /**
     * 楼层数量
     */
    @Column(name = "floor_count")
    @Comment("楼层数量")
    private Integer floorCount;

    /**
     * 建成年份
     */
    @Column(name = "build_year")
    @Comment("建成年份")
    private Integer buildYear;

    /**
     * 管理单位名称
     */
    @Column(name = "management_unit", length = 100)
    @Comment("管理单位名称")
    private String managementUnit;

    /**
     * 楼宇当前状态
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "status", nullable = false, length = 20)
    @Comment("楼宇当前状态")
    private BuildingStatus status = BuildingStatus.NORMAL;

    /**
     * 记录创建时间
     */
    @Column(name = "create_time", nullable = false, updatable = false)
    @Comment("记录创建时间")
    private LocalDateTime createTime;

    /**
     * 记录最后更新时间
     */
    @Column(name = "update_time", nullable = false)
    @Comment("记录最后更新时间")
    private LocalDateTime updateTime;

    /**
     * 创建人用户ID
     */
    @Column(name = "create_user")
    @Comment("创建人用户ID")
    private Long createUser;

    /**
     * 最后更新人用户ID
     */
    @Column(name = "update_user")
    @Comment("最后更新人用户ID")
    private Long updateUser;

    /**
     * 乐观锁版本号
     */
    @Version
    @Column(name = "version", nullable = false)
    @Comment("乐观锁版本号")
    private Integer version = 1;

    /**
     * 软删除标记
     */
    @Column(name = "is_deleted", nullable = false)
    @Comment("软删除标记")
    private Boolean isDeleted = false;

    /**
     * 删除时间
     */
    @Column(name = "delete_time")
    @Comment("删除时间")
    private LocalDateTime deleteTime;

    @PrePersist
    protected void onCreate() {
        LocalDateTime now = LocalDateTime.now();
        this.createTime = now;
        this.updateTime = now;
    }

    @PreUpdate
    protected void onUpdate() {
        this.updateTime = LocalDateTime.now();
    }

}